<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Photos extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('manage_login');
        $this->load->model('share_media');
        $this->load->model('manage_comments');
        $this->load->model('manage_follow');
        $this->load->model('get_profile_infos');
    }
    public function index() {
        $photoID = $this->uri->segment(2);
        if ($photoID) {
            $this->photo($photoID);
        } else {            
            $data = $this->manage_login->manageLogin();
            $photos = $this->share_media->getPhotos();
            $data['photos'] = $photos;
            $this->lang->load('main', 'english');
            $this->load->view('photos/photos', $data);
        }
    }
    function photo($photoID){
        $photo = $this->share_media->getPhoto($photoID);
        $user = $this->get_profile_infos->getInfosID($photo[0]["user_id"]);
        $likes = $this->getLikes($photoID);
        $favs = $this->getFavs($photoID);
        $didLike = $this->share_media->didLike($photoID,$this->input->cookie('LEAPSESSID'));
        $didFav = $this->share_media->didFav($photoID,$this->input->cookie('LEAPSESSID'));
        $comments = $this->manage_comments->getcomments('photo',$photoID);
        $data = $this->manage_login->manageLogin();
        $data['isFollowing'] = $this->manage_follow->isFollowing($this->input->cookie('LEAPSESSID'),$photo[0]["user_id"]);
        $data['photo_infos'] = $photo[0];
        $data['user'] = $user;
        $data['likes'] = $likes;
        $data['favs'] = $favs;
        $data['did_like'] = $didLike;
        $data['did_fav'] = $didFav;
        $data['comments'] = $comments;
        $data['connected'] = $this->input->cookie('LEAPSESSID');
        $data['facebook_og'] = '<meta property="fb:app_id" content="264489850252668" /> 
              <meta property="og:type" content="leap_pk:parkour_photo" /> 
              <meta property="og:title" content="'.$data['photo_infos']['title'].'" /> 
              <meta property="og:image" content="'.$data['photo_infos']['thumb'].'" /> 
              <meta property="og:description" content="'.$data['photo_infos']['description'].'" /> 
              <meta property="og:url" content="http://leapdemo.com/photos/'.$data['photo_infos']['photo_id'].'">';
        $this->load->view('photos/photo', $data);
    }
    function xhrPhoto(){
        $photoID = $this->input->get("id");
        $photo = $this->share_media->getPhoto($photoID);
        $user = $this->get_profile_infos->getInfosID($photo[0]["user_id"]);
        $likes = $this->getLikes($photoID);
        $favs = $this->getFavs($photoID);
        $didLike = $this->share_media->didLike($photoID,$this->input->cookie('LEAPSESSID'));
        $didFav = $this->share_media->didFav($photoID,$this->input->cookie('LEAPSESSID'));
        $comments = $this->manage_comments->getcomments('photo',$photoID);
        $data['photo'] = $photo[0];
        $data['user'] = $user;
        $data['likes'] = $likes;
        $data['favs'] = $favs;
        $data['did_like'] = $didLike;
        $data['did_fav'] = $didFav;
        $data['comments'] = $comments;
        $data['connected'] = $this->input->cookie('LEAPSESSID');
        echo json_encode($data);
    }
    function like(){
        $photo_id = $this->input->post("photo_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->likePhoto($photo_id,$user_id);
        echo $query;
    }
    function dislike(){
        $photo_id = $this->input->post("photo_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->dislikePhoto($photo_id,$user_id);
        echo $query;
    }
    function favorite(){
        $photo_id = $this->input->post("photo_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->favoritePhoto($photo_id,$user_id);
        echo $query;
    }
    function dlFavorite(){
        $photo_id = $this->input->post("photo_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->rmvFavorite($photo_id,$user_id);
        echo $query;
    }
    function removeLike(){

    }
    function getLikes($photo_id){
      
      $likes = $this->share_media->getLikes($photo_id);
      return $likes;
    }
    function getFavs($photo_id){
      
      $favs = $this->share_media->getFavs($photo_id);
      return $favs;
    }
}
